package com.imooc.mall.listener;

import com.google.gson.Gson;
import com.imooc.mall.pojo.PayInfo;
import com.imooc.mall.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "payQueue")
@Slf4j
public class PayMsgListener {

    @Autowired
    private IOrderService orderService;

    /**
     * 关于Pay传递过来的消息，转成PayInfo对象，正确姿势应该是：
     * pay项目提供client.jar,mall项目引入jar包
     * 这里简单处理，直接把pay项目的PayInfo拷贝过来
     *
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        log.info("接受到消息==>>{}",msg);
        PayInfo payInfo = new Gson().fromJson(msg, PayInfo.class);
        if (payInfo.getPlatformStatus().equals("SUCCESS")) {
            //支付状态是成功的话，修改订单里的状态
            orderService.paid(payInfo.getOrderNo());
        }
    }


}
